package com.cskaoyan.controller.backgroundsystemcontroller;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.service.SystemService;
import com.cskaoyan.utils.FileUtil;
import com.cskaoyan.utils.Md5Util;
import lombok.SneakyThrows;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @author thz
 * @class SystemController
 */
@RestController
@RequestMapping("admin")
public class SystemController {

    @Autowired
    SystemService systemService;

    /**
     * @author thz
     * @title adminList
     * @description: 查询管理员信息
     * @param: [adListBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @RequestMapping("admin/list")
    @RequiresPermissions(value = {"*","admin:admin:list" },logical = Logical.OR)
    public BaseRespVo listAdmin(AdminListBO adminListBO) {
        BaseData<AdminListItemVO> baseData = systemService.queryAdminInfo(adminListBO);
        return BaseRespVo.ok(baseData);
    }

    @SneakyThrows
    @RequestMapping("admin/create")
    @RequiresPermissions(value = {"*","admin:admin:create" },logical = Logical.OR)
    public BaseRespVo adminCreate(@RequestBody AdminCreateBO adminCreateBO, HttpServletRequest httpServletRequest) {
        AdminCreateVO adminCreateVO = systemService.addAdmin(adminCreateBO);
        return BaseRespVo.ok(adminCreateVO);
    }

    @RequestMapping("admin/update")
@RequiresPermissions(value = {"*","admin:admin:update" },logical = Logical.OR)
    public BaseRespVo adminUpdate(@RequestBody AdminUpdateBO adminUpdateBO, HttpServletRequest httpServletRequest) {
        AdminUpdateVo adminUpdateVo = systemService.changeAdmin(adminUpdateBO);
        return BaseRespVo.ok(adminUpdateVo);
    }

    @RequiresPermissions(value = {"*","admin:admin:delete" },logical = Logical.OR)
    @RequestMapping("admin/delete")
    public BaseRespVo adminDelete(@RequestBody AdminDeleteBO adminDeleteBO, HttpServletRequest httpServletRequest) {
        systemService.removeAdmin(adminDeleteBO);
        return BaseRespVo.ok();
    }


    @RequestMapping("log/list")
    @RequiresPermissions(value = {"*","admin:log:list" },logical = Logical.OR)
    public BaseRespVo logList(LogListBO logListBO) {
        BaseData<LogListItemVO> baseData = systemService.queryLog(logListBO);
        return BaseRespVo.ok(baseData);
    }

    @RequestMapping("role/list")
    @RequiresPermissions(value = {"*","admin:role:list" },logical = Logical.OR)
    public BaseRespVo roleList(RoleListBO roleListBO) {
        BaseData<RoleListItemVO> baseData = systemService.queryRoleList(roleListBO);
        return BaseRespVo.ok(baseData);
    }

    @RequestMapping("role/options")
    public BaseRespVo roleOptions() {
        List<RoleOptionsVO> roleOptionsVOS = systemService.queryRole();
        return BaseRespVo.ok(roleOptionsVOS);
    }

    @RequestMapping("role/create")
    @RequiresPermissions(value = {"*","admin:role:create" },logical = Logical.OR)
    public BaseRespVo roleCreate(@RequestBody RoleCreateBO roleCreateBO) {
        RoleCreateVO roleCreateVO = systemService.createRole(roleCreateBO);
        return BaseRespVo.ok(roleCreateVO);
    }

    @RequestMapping("role/update")
    @RequiresPermissions(value = {"*","admin:role:update" },logical = Logical.OR)
    public BaseRespVo roleUpdate(@RequestBody RoleUpdateBO roleUpdateBO) {
        systemService.changeRole(roleUpdateBO);
        return BaseRespVo.ok();
    }

    @RequestMapping("role/delete")
    @RequiresPermissions(value = {"*","admin:role:delete" },logical = Logical.OR)
    public BaseRespVo roleDelete(@RequestBody RoleDeleteBO roleDeleteBO) {
        systemService.removeRole(roleDeleteBO);
        return BaseRespVo.ok();
    }

    @GetMapping("role/permissions")
    @RequiresPermissions(value = {"*","admin:role:permissions" },logical = Logical.OR)
    public BaseRespVo rolePermissionGet(Integer roleId) {
        RolePermissionVO rolePermissionVO = systemService.queryRolePermission(roleId);
        return BaseRespVo.ok(rolePermissionVO);
    }
    @RequiresPermissions(value = {"*","admin:role:permissions" },logical = Logical.OR)
    @PostMapping("role/permissions")
    public BaseRespVo rolePermissionPost(@RequestBody RolePermissionPostBO rolePermissionPostBO) {
        systemService.changeRolePermission(rolePermissionPostBO);
        return BaseRespVo.ok();
    }

    @RequestMapping("storage/list")
    @RequiresPermissions(value = {"*","admin:storage:list" },logical = Logical.OR)
    public BaseRespVo storageList(StorageListBO storageListBO) {
        BaseData<StorageListItemVO> baseData = systemService.queryStorage(storageListBO);
        return BaseRespVo.ok(baseData);
    }

    @SneakyThrows
    @RequestMapping("storage/create")
    @RequiresPermissions(value = {"*","admin:storage:create" },logical = Logical.OR)
    public BaseRespVo storageCreate(MultipartFile file) {
        String homePath = "D:\\spring\\image\\wx\\storage\\fetch";
        File newFile = new File(homePath + "\\" + UUID.randomUUID().toString() +
                file.getContentType().replace("image/", "."));

        byte[] bytes = file.getBytes();
        String md5;
        md5 = Md5Util.getMd5(new String(bytes));
        File homeFile = new File(homePath);
        File[] files = homeFile.listFiles();
        if (files != null) {
            for (File f : files) {
                byte[] bytes1 = FileUtil.toByteArray(f);

                String md51;
                md51 = Md5Util.getMd5(new String(bytes1));
                if (md5.equals(md51)) {
                    StorageCreateVO storageCreateVO = systemService.addStorage(f, file);
                    return BaseRespVo.ok(storageCreateVO);
                }
            }
        }

        file.transferTo(newFile);
        StorageCreateVO storageCreateVO = systemService.addStorage(newFile, file);
        return BaseRespVo.ok(storageCreateVO);
    }

    @RequestMapping("storage/update")
    @RequiresPermissions(value = {"*","admin:storage:update" },logical = Logical.OR)
    public BaseRespVo storageUpdate(@RequestBody StorageUpdateBo storageUpdateBo) {
        StorageUpdateVO storageUpdateVO = systemService.changeStorage(storageUpdateBo);
        return BaseRespVo.ok(storageUpdateVO);
    }

    @RequestMapping("storage/delete")
    @RequiresPermissions(value = {"*","admin:storage:delete" },logical = Logical.OR)
    public BaseRespVo storageDelete(@RequestBody StorageDeleteBO storageDeleteBO) {
        systemService.removeStorage(storageDeleteBO);
        return BaseRespVo.ok();
    }

}

